// 1. 导入路由组件.
import { createRouter, createWebHashHistory } from 'vue-router'
import LoginRegister from "~/pages/log/index.vue"
import Index from "~/pages/home/index.vue"
import Dashboard from "~/pages/home/dashboard.vue"
import Admin from "~/pages/Layout/index.vue"
import MenuList from "~/pages/menu/index.vue"
import CompanyList from "~/pages/company/index.vue"
import EmployeeList from "~/pages/company/employee.vue"
import DepartmentList from "~/pages/company/department.vue"
import RoleList from "~/pages/role/index.vue"
import UserList from "~/pages/user/index.vue"
import PlateFromList from "~/pages/platefrom/index.vue"
import SkuList from "~/pages/sku/index.vue"
import SkuBind from "~/pages/sku/bind.vue"
import MessageList from "~/pages/message/index.vue"
import CompanyMessageList from "~/pages/company/message.vue"
import UserPlatefrom from "~/pages/user/platefrom.vue"
import MediasList from "~/pages/medias/index.vue"
import LabelTypeList from "~/pages/label_type/index.vue"
import ContentTypeList from "~/pages/content_type/index.vue"
import ContentLabelList from "~/pages/content_label/index.vue"
import Auth from "~/pages/auth/auth.vue"
import AuthCompany from "~/pages/auth/auth_company.vue"
import Agreement from "~/pages/auth/agreement.vue"
import userHome from "~/pages/user/home.vue"



// 2. 定义一些路由-默认路由
const routes = [
    {
        path: "/",
        name: 'admin',
        component: Admin,
    },
    {
        path: "/login",
        component: LoginRegister,
        meta: { title: "登录页" }
    },
    {
        path: "/agreement",
        name:"/agreement",
        component: Agreement,
        meta: { title: "用户协议" }
    },
    {
        path: "/auth_company",
        name:"/auth_company",
        component: AuthCompany,
        meta: { title: "用户认证" }
    },
    
    {
        path: "/auth",
        name:"/auth",
        component: Auth,
        meta: { title: "用户认证" }
    },
    // {
    //     path: "/user/user_home",
    //     name:"/user/user_home",
    //     component: userHome,
    //     meta: { title: "用户中心" }
    // },
]

// 动态路由
const asyncRoutes = [
    {
        path: "/",
        name: "/",
        component: Dashboard,
        meta: { title: "后台首页" }
    },
    {
        path: "/menu/list",
        component: MenuList,
        meta: { title: "菜单列表" }
    },
    {
        path: "/company/list",
        component: CompanyList,
        meta: { title: "公司列表" }
    },
    {
        path: "/company/department",
        component: DepartmentList,
        meta: { title: "部门列表" }
    },
    {
        path: "/employee/list",
        component: EmployeeList,
        meta: { title: "员工列表" }
    },
    
    {
        path: "/role/list",
        component: RoleList,
        meta: { title: "角色列表" }
    },
    {
        path: "/user/list",
        component: UserList,
        meta: { title: "用户列表" }
    },
    {
        path: "/platefrom/list",
        component: PlateFromList,
        meta: { title: "平台列表" }
    },
    {
        path: "/sku/list",
        name:"/sku/list",
        component: SkuList,
        meta: { title: "套餐列表" }
    },
    {
        path: "/sku/bind",
        name: "/skus/bind",
        component: SkuBind,
        meta: { title: "套餐绑定" }
    },
    {
        path: "/message/list",
        name:"/message/list",
        component: MessageList,
        meta: { title: "消息列表" }
    },
    {
        path: "/user/message",
        name:"/user/message",
        component: CompanyMessageList,
        meta: { title: "公司消息" }
    },
    {
        path: "/user/platefrom",
        name:"/user/platefrom",
        component: UserPlatefrom,
        meta: { title: "账户平台" }
    },
    {
        path: "/media/list",
        name:"/media/list",
        component: MediasList,
        meta: { title: "媒体列表" }
    },
    {
        path: "/system/label_type",
        name:"/system/label_type",
        component: LabelTypeList,
        meta: { title: "标签类型" }
    },
    {
        path: "/medias/content_type",
        name:"/medias/content_type",
        component: ContentTypeList,
        meta: { title: "内容类型" }
    },
    {
        path: "/medias/content_label",
        name:"/medias/content_label",
        component: ContentLabelList,
        meta: { title: "内容标签" }
    },
    {
        path: "/user/user_home",
        name:"/user/user_home",
        component: userHome,
        meta: { title: "用户中心" }
    },


]



// 3. 创建路由实例并传递 `routes` 配置
export const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),
    routes, // `routes: routes` 的缩写
})



// 动态添加路由的方法
export function addRoutes(menus) {
    // 是否有新的路由
    let hasNewRoutes = false
    const findAndAddRoutesByMenus = (arr) => {
        arr.forEach(e => {
            let item = asyncRoutes.find(o => o.path == e.frontpath)
            if (item && !router.hasRoute(item.path)) {
                router.addRoute("admin", item)
                hasNewRoutes = true
            }
            if (e.children && e.children.length > 0) {
                findAndAddRoutesByMenus(e.children)
            }
        })
    }

    findAndAddRoutesByMenus(menus)

    return hasNewRoutes
}